package Groep3;


import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;

import sun.awt.image.URLImageSource;


public class Ripper {
	private ArrayList<String> urls;
	private String url; 
	
	public Ripper() throws URISyntaxException{
		urls = new ArrayList<String>();
		Scanner in= new Scanner(System.in);
		System.out.println("Voer website in");
		url = in.nextLine();
		rip(url);
		System.out.println("Website geript");
		in.close();
	}
	
	public String grabContent(URL url) throws  IOException{
		URLConnection conn = url.openConnection();
		
		StringBuffer stringbuffer = new StringBuffer();
		try{
			BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream()));
		String inputline = null;
		
		while((inputline = in.readLine())!= null){
			stringbuffer.append(inputline + '\n');
		}
		in.close();
		//System.out.println(urls);
		}catch(FileNotFoundException e){
			System.err.println(url+ " bestaat niet");
		}catch(IOException e){
			
		}
		return stringbuffer.toString();
	}
	
	public void rip(String uri) throws URISyntaxException{
		System.out.println(uri);
		String content = null;
			URL uRl = null;
		try {
			if(url.contains("http://")){
				uRl = new URL(uri);
			}else{
				url = "http://" + uri;
			uRl = new URL(url);
			}
			urls.add(uRl.toString());
			System.out.println("url in rip "+ uRl);
			
			//Zoekt naar nieuwe urls		
			search(uRl);
			for(int i = 0;i < urls.size(); i++){
				System.out.println("i:"+ i + " index: " + urls.size()+ " url: "+ urls.get(i));
				URL searchUrl = new URL(urls.get(i));
				if(uri.toLowerCase().endsWith(".jpg") || uri.toLowerCase().endsWith(".png") || uri.endsWith(".gif") || uri.endsWith(".svg")){
					System.err.println("plaatje kon niet worden opgelagen");
				}else{
					content = grabContent(searchUrl);
					search(searchUrl);
					if(searchUrl.toString().contains("mailto")){
						continue;
					}else{
						save(new URL(urls.get(i)), content);
					}
				}
			}
			FileOutputStream fs = new FileOutputStream(uRl.getHost()+  "/sitemap.txt");
			for(int i = 0;i < urls.size(); i++){
				fs.write((urls.get(i)+ '\n').getBytes());
			}
			fs.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	
	public void save( URL url, String content ) throws IOException{
		String path = url.getPath();
//		Image image = null;
		URLImageSource content1 = null;
		//createPath(url);
		String fullLowerCasePath = path.toLowerCase();
		if(fullLowerCasePath.endsWith(".jpg") || fullLowerCasePath.endsWith(".png") || fullLowerCasePath.endsWith(".gif") || fullLowerCasePath.endsWith(".svg")) {
			//Object o = url.getContent();
			//content1 = new URLImageSource(url);
			System.err.println("plaatje "+ fullLowerCasePath + " kon niet worden opgelagen");
		}
		File dir = new File(url.getHost());
		if ( !dir.exists() && !dir.mkdir() ){
			throw new IOException("Cannot create dir");
		}
	
		// Omzetten naar .html
		path = path.replace(".php", ".html");
	
		// Voor elke slash, new dir maken
		// Laatste hoeft niet
		String[] splitted = path.split("/");
		if ( splitted.length > 0){
			splitted[splitted.length -1] = "";
			String dirPath = url.getHost() + "/";
			for( String part : splitted){
				if ( part.length() > 0 ){
					File dir2 = new File(dirPath + part);
				
				if ( !dir2.exists() && !dir2.mkdir() )
					throw new IOException("Cannot create dir");
				
				dirPath += part + "/";
			}
		}
	}
		// Als je bijv. naar http://test.nl/dir gaat terwijl het eigenlijk http://test.nl/dir/index.html is
		File isDir = new File(url.getHost() + path);
		if ( isDir.isDirectory() )
		path += "/index.html";
//		BufferedImage image = null;
//		String extension = "gif";
//		try {
//		  image = ImageIO.read(url);
//		} catch (IOException ex) {
//		  System.out.println("can not read : " + url);
//		  System.out.println(ex.getMessage());
//		}
//		
//		try {
//		  ImageIO.write(image, extension, isDir);
//		} catch (IOException e) {
//		  e.printStackTrace();
//		} catch (IllegalArgumentException a) {
//		  a.printStackTrace();
//		}
			FileOutputStream fstream = new FileOutputStream(url.getHost()+ path );
	
		fstream.write(content.getBytes());
			fstream.close();
	}	
	
	public void getImage(URL url, File isDir) {
		BufferedImage image = null;
		String extension = "gif";
		try {
		  image = ImageIO.read(url);
		} catch (IOException ex) {
		  System.out.println("can not read : " + url);
		  System.out.println(ex.getMessage());
		}
		
		try {
		  ImageIO.write(image, extension, isDir);
		} catch (IOException e) {
		  e.printStackTrace();
		} catch (IllegalArgumentException a) {
		  a.printStackTrace();
		}
	}


	public void search(URL uRl) throws  MalformedURLException, IOException{
		String content = grabContent(uRl);
		String newURI = "";
		//String cont = "";
		Pattern pattern = Pattern.compile("(src|href)=('|\")?(([a-z0-9A-Z]|/|_|:|-|\\.)+)('|\"|<)?" );
		// finds a viewstate value in the string with regular expression
		Matcher matcher = pattern.matcher(content);
		//System.out.println(content);
		while (matcher.find()) { 
			try{
				newURI = new URI("http://"+new URI(url).getHost()+ "/").resolve(new URI(matcher.group(3))).toString();
								//System.out.println(newURI);
				// Geen externe links!
				if ( newURI.contains("http://") && !newURI.contains(uRl.getHost())) {
					System.out.println(newURI);
					continue;

				}else{
				try{
						//cont = grabContent(new URL("http://"+url.toString()+"/"+ newURI));
					
				
					System.out.println("gevonden links: "+ newURI);
						if(!urls.contains(newURI)){
							//rip(newURI);
							urls.add( newURI);					
						}
					}catch(IllegalArgumentException e){
						e.getCause();
						System.err.println(newURI + " kan niet worden gedownload");
					}
				}
			}catch(URISyntaxException e){
				e.printStackTrace();			
			}
		}
	}
	

	/**
	 * @param args
	 * @throws URISyntaxException 
	 */
	public static void main(String[] args) throws URISyntaxException {
			new Ripper();
			
	}
	
}
